package yxy.game.pm2.module.playtype;

import org.apache.commons.lang3.StringUtils;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.battle.module.PropertyType;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.*;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.Sign;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.avatar.TitleModule;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.camptower.FourSealModule;
import yxy.game.pm2.module.capturestrategicpoints.CaptureStrategicPointsModule;
import yxy.game.pm2.module.godBeast.GodBeast;
import yxy.game.pm2.module.godBeast.GodBeastEquip;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;
import yxy.game.pm2.module.niubee.NiuBeeModule;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;
import yxy.game.pm2.module.playerCommand.PlayerCommandModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;

import java.util.*;

public class PlayTypeHandle {


    //todo ------------------------------------------玉玺------------------------------------------

    /**
     * 对六个阵营战力计算缓存
     */
    public static void saveJadeSeal(String playerCode, PlayTypeModule playTypeModule) {

        JadeSealModule jsModule = JadeSealModule.get(playerCode);
        HashMap<Integer, PropertyData> JadeSeal = new HashMap<Integer, PropertyData>(); //玉玺  key 阵营
        //1:魏 2:蜀 3:吴 4:群 5:神 6:魔
        for (int i = 1; i <= 6; i++) {
            PropertyData propertyData = new PropertyData();
            JadeSeal js = jsModule.getJss().get(i); // 所属阵营玉玺
            if (!js.isOpen()) {
                JadeSeal.put(i, propertyData);
                continue;
            }

            // 等级
            int mgid = js.countmgid();
            medalgrade mg = DataCenter.getData(mgid, medalgrade.class);
            if (mg != null) {
                int attack = mg.getAttack() + mg.getAttackup() * js.getExp();
                int life = mg.getLife() + mg.getLifeup() * js.getExp();
                propertyData.addValue(PropertyType.P_攻击, attack);
                propertyData.addValue(PropertyType.P_生命, life);
            }

            // 阶
            int mrid = js.countmrid();
            medalrefine mr = DataCenter.getData(mrid, medalrefine.class);
            if (mr != null) {
                propertyData.addValue(PropertyType.P_攻击, mr.getAttack()); // 攻击 attack
                propertyData.addValue(PropertyType.P_防御, mr.getPdefense()); // 防御 pdefense
                propertyData.addValue(PropertyType.P_法防, mr.getSefense()); // 法防 sefense
                propertyData.addValue(PropertyType.P_生命, mr.getLife()); // 生命 life
            }

            JadeSeal.put(i, propertyData);
        }
        playTypeModule.setJadeSeal(JadeSeal);
        playTypeModule.set();
    }
    //todo ------------------------------------------玉玺------------------------------------------


    //todo ------------------------------------------神兽------------------------------------------

    /**
     * 对神兽战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void savePickedGodBeast(String playerCode, PlayTypeModule playTypeModule) {
        PickedGodBeastModule pgbModule = PickedGodBeastModule.get(playerCode);

        if (pgbModule == null) {
            return;
        }

        PropertyData propertyData = new PropertyData();

        for (GodBeast godBeast : pgbModule.getGbs().values()) {

            if (!godBeast.isOpen()) continue;//未开启就跳过

            int attack = 0;
            int life = 0;
            int defense = 0;

            attack = godBeast.getAttack();
            life = godBeast.getLife();
            defense = godBeast.getDefense();

            for (GodBeastEquip godBeastEquip : godBeast.getGodBeastEquips()) {
                //穿戴装备
                if (godBeastEquip != null) {
                    if (godBeastEquip.getAttributetype() == 1) { //攻击
                        attack += godBeastEquip.getAttributenume();
                    } else if (godBeastEquip.getAttributetype() == 2) { //生命
                        life += godBeastEquip.getAttributenume();
                    } else if (godBeastEquip.getAttributetype() == 3) { //防御
                        defense += godBeastEquip.getAttributenume();
                    }
                }
            }

            propertyData.addValue(PropertyType.P_攻击, attack);
            propertyData.addValue(PropertyType.P_防御, defense);
            propertyData.addValue(PropertyType.P_生命, life);
        }

        playTypeModule.setTherion(propertyData);
        playTypeModule.set();
    }
    //todo ------------------------------------------神兽------------------------------------------


    //todo ------------------------------------------联盟------------------------------------------

    /**
     * 对联盟战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void saveUnion(String playerCode, PlayTypeModule playTypeModule) {
        UnionPlayer unionPlayer = UnionPlayer.get(playerCode);
        HashMap<Integer, PropertyData> union = new HashMap<Integer, PropertyData>(); //联盟  key角色定位
        if (unionPlayer == null || unionPlayer.getUnionCode() == null) {
            playTypeModule.setUnion(union);
            playTypeModule.set();
            return;
        }
        if (UnionBean.get(unionPlayer.getUnionCode()) == null) {
            playTypeModule.setUnion(union);
            playTypeModule.set();
            return;
        }

        UnionPlayerTech tech = unionPlayer.getTech();

        for (int i = 1; i <= 5; i++) {
            PropertyData property = tech.property(i);
            union.put(i, property);
        }

        playTypeModule.setUnion(union);
        playTypeModule.set();
    }
    //todo ------------------------------------------联盟------------------------------------------


    //todo ------------------------------------------称号------------------------------------------

    /**
     * 对称号战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void saveTitle(String playerCode, PlayTypeModule playTypeModule) {
        PropertyData propertyData = new PropertyData();
        ArrayList<String> list = new ArrayList<>();//titleId  list
        Collection<TitleModule> all = TitleModule.all(playerCode);

        //从新的缓存获取
        Iterator<TitleModule> iterator = all.iterator();
        while (iterator.hasNext()) {
            TitleModule next = iterator.next();
            String dataCode = next.getDataCode();
            if (!list.contains(dataCode)) {
                list.add(dataCode);
            }
        }

        //兼容的操作
        Set<String> allTitle = Sign.all(playerCode, CurrencyType.CT_称号);
        for (String dataCode : allTitle) {
            if (!list.contains(dataCode)) {
                list.add(dataCode);
            }
        }

        if (list.size() == 0) {
            playTypeModule.setTitle(propertyData);
            playTypeModule.set();
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            title data = DataCenter.getData(list.get(i), title.class);
            if (data == null || data.getJudge() <= 0) continue;
            for (int y = 0; y < data.getSkilltype().length; y++) {
                int type = data.getSkilltype()[y];
                int value = data.getPromote()[y];
                PropertyType propertyType = PropertyType.forNumber(type);
                if (propertyType == null || value == 0) {
                    continue;
                }
                propertyData.addValue(propertyType, value);
            }
        }
        playTypeModule.setTitle(propertyData);
        playTypeModule.set();
    }
    //todo ------------------------------------------称号------------------------------------------


    //todo ------------------------------------------形象(目前功能暂时没用)------------------------------------------

    /**
     * 对形象战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void saveAvatar(String playerCode, PlayTypeModule playTypeModule) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        int avatarId = playerBean.getAvatar();
        avatar data = DataCenter.getData(avatarId, avatar.class);
        if (data == null || data.getJudge() <= 0) {
            return;
        }
        PropertyData propertyData = new PropertyData();
        for (int i = 0; i < data.getSkilltype().length; i++) {
            int type = data.getSkilltype()[i];
            int value = data.getPromote()[i];
            PropertyType propertyType = PropertyType.forNumber(type);
            if (propertyType == null || value == 0) {
                continue;
            }
            propertyData.addValue(propertyType, value);
        }
        playTypeModule.setAvatar(propertyData);
        playTypeModule.set();
    }
    //todo ------------------------------------------形象(目前功能暂时没用)------------------------------------------


    //todo ------------------------------------------神器------------------------------------------

    /**
     * 对神器战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void saveWeapon(String playerCode, PlayTypeModule playTypeModule) {
        PropertyData propertyData = new PropertyData();
        NiuBeeModule niuBeeModule = NiuBeeModule.get(playerCode);
        if (niuBeeModule == null) {
            return;
        }
        // 等级
        weapongrade _weapongrade = DataCenter.getData(niuBeeModule.getLevel(), weapongrade.class);
        if (_weapongrade != null) {
            int attack = _weapongrade.getAttack() + _weapongrade.getAttackup() * niuBeeModule.getExp();
            int life = _weapongrade.getLife() + _weapongrade.getLifeup() * niuBeeModule.getExp();
            propertyData.addValue(PropertyType.P_攻击, attack);
            propertyData.addValue(PropertyType.P_生命, life);
        }
        // 精炼
        weaponrefine _weaponrefine = DataCenter.getData(niuBeeModule.getRefineLevel(), weaponrefine.class);
        if (_weaponrefine != null) {
            propertyData.addValue(PropertyType.P_攻击, _weaponrefine.getAttack()); // 攻击 attack
            propertyData.addValue(PropertyType.P_防御, _weaponrefine.getPdefense()); // 防御 pdefense
            propertyData.addValue(PropertyType.P_法防, _weaponrefine.getSefense()); // 法防 sefense
            propertyData.addValue(PropertyType.P_生命, _weaponrefine.getLife()); // 生命 life
            propertyData.addValue(PropertyType.P_暴击, _weaponrefine.getStrike()); // 暴击 strike
            propertyData.addValue(PropertyType.P_抗暴, _weaponrefine.getAntiriot()); // 抗暴 antiriot
            propertyData.addValue(PropertyType.P_暴伤, _weaponrefine.getBurstdps()); // 暴伤 burstdps
            propertyData.addValue(PropertyType.P_命中, _weaponrefine.getFit()); // 命中 fit
            propertyData.addValue(PropertyType.P_闪避, _weaponrefine.getDodge()); // 闪避 dodge
            propertyData.addValue(PropertyType.P_控制, _weaponrefine.getControl()); // 控制 control
            propertyData.addValue(PropertyType.P_抗控, _weaponrefine.getResist()); // 抗控 resist
        }
        // 铸魂
        if (niuBeeModule.getSoulLevel() > 0) {
            int attack = BattleHandle.神兵铸魂升级攻击属性提升;
            int life = BattleHandle.神兵铸魂升级生命属性提升;
            int pdefense = BattleHandle.神兵铸魂升级防御属性提升;
            attack *= niuBeeModule.getSoulLevel();
            life *= niuBeeModule.getSoulLevel();
            pdefense *= niuBeeModule.getSoulLevel();
            propertyData.addValue(PropertyType.P_攻击, attack);
            propertyData.addValue(PropertyType.P_生命, life);
            propertyData.addValue(PropertyType.P_防御, pdefense);
        }
        playTypeModule.setWeapon(propertyData);
        playTypeModule.set();
    }
    //todo ------------------------------------------神器------------------------------------------


    //todo ------------------------------------------兵符和兵符槽------------------------------------------

    /**
     * 对兵符和兵符槽战力计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void savePickedTroop(String playerCode, PlayTypeModule playTypeModule) {

        PickedTroopsModule module = PickedTroopsModule.get(playerCode);//兵符槽缓存
        if (module == null) return;

        HashMap<Integer, PropertyData> PickedTroop = new HashMap<Integer, PropertyData>(); //兵符和兵符槽  key 阵营

        //1:魏 2:蜀 3:吴 4:群 5:神 6:魔
        for (int i = 1; i <= 6; i++) {
            PropertyData propertyData = new PropertyData();
            PickedTroops pickedTroops = module.getPts().get(i);// 兵符槽
            if (pickedTroops != null && pickedTroops.isOpen()) {
                PickedTroopsSlot slot = PickedTroopsSlot.get(playerCode, pickedTroops.getPickedTroopsSolt());//兵符
                //兵符属性添加
                commandertally commandertally = DataCenter.getData(slot.getDataId(), commandertally.class);
                if (commandertally != null) {
                    int attack = commandertally.getAttack();
                    int defense = commandertally.getDefense();
                    int life = commandertally.getLife();
                    propertyData.addValue(PropertyType.P_攻击, attack);
                    propertyData.addValue(PropertyType.P_防御, defense);
                    propertyData.addValue(PropertyType.P_生命, life);
                }

                //兵符槽属性增加
                int level = pickedTroops.getLevel();
                commandertallylevel commandertallylevel = DataCenter.getData(i + "_" + level, commandertallylevel.class);
                if (commandertallylevel != null) {
                    int attack = commandertallylevel.getAttack();
                    int defense = commandertallylevel.getDefense();
                    int life = commandertallylevel.getLife();
                    propertyData.addValue(PropertyType.P_攻击, attack);
                    propertyData.addValue(PropertyType.P_防御, defense);
                    propertyData.addValue(PropertyType.P_生命, life);
                }
            }
            PickedTroop.put(i, propertyData);
        }

        //兵符槽词条属性加成
        HashMap<Integer, PropertyData> pickedTroopSlot = new HashMap<Integer, PropertyData>();//兵符词条属性  key 目标对象 1.前排   2.后排  3.全体
        //词条加成
        for (PickedTroops troops : module.getPts().values()) {
            if (StringUtils.isEmpty(troops.getPickedTroopsSolt())) continue;
            PickedTroopsSlot bingfu = PickedTroopsSlot.get(playerCode, troops.getPickedTroopsSolt());//兵符
            computePropertryForEntry(bingfu.getEntryId(), bingfu.getValue(), pickedTroopSlot);
        }

        playTypeModule.setPickedTroop(PickedTroop);
        playTypeModule.setPickedTroopSlot(pickedTroopSlot);
        playTypeModule.set();
    }

    private static void computePropertryForEntry(ArrayList<Integer> entryId, ArrayList<Integer> value, HashMap<Integer, PropertyData> pickedTroopSlot) {
        for (int i = 0; i < entryId.size(); i++) {
            commandertallyrandom data = DataCenter.getData(entryId.get(i), commandertallyrandom.class);
            int attributetype = data.getAttributetype();//属性类型
            int target = data.getTarget();//目标   1.前排   2.后排  3.全体
            switch (target) {
                case 1:
                    //该英雄属于前排则加属性
                    PropertyData propertyData = pickedTroopSlot.get(target) == null ? new PropertyData() : pickedTroopSlot.get(target);
                    propertyData.addValue(PropertyType.forNumber(attributetype), value.get(i));
                    pickedTroopSlot.put(target, propertyData);
                    break;
                case 2:
                    //该英雄属于后排则加属性
                    propertyData = pickedTroopSlot.get(target) == null ? new PropertyData() : pickedTroopSlot.get(target);
                    propertyData.addValue(PropertyType.forNumber(attributetype), value.get(i));
                    pickedTroopSlot.put(target, propertyData);
                    break;
                case 3:
                    //全体加
                    propertyData = pickedTroopSlot.get(target) == null ? new PropertyData() : pickedTroopSlot.get(target);
                    propertyData.addValue(PropertyType.forNumber(attributetype), value.get(i));
                    pickedTroopSlot.put(target, propertyData);
                    break;
            }
        }
    }

    //todo ------------------------------------------兵符和兵符槽------------------------------------------

    //todo ------------------------------------------主公统御加点------------------------------------------

    /**
     * 对主公统御加点计算缓存
     *
     * @param playerCode
     * @param playTypeModule
     */
    public static void savePlayerCommand(String playerCode, PlayTypeModule playTypeModule) {
        PlayerCommandModule module = PlayerCommandModule.get(playerCode);

        if (module == null) {
            return;
        }

        PropertyData propertyData = new PropertyData();

        int attackLevel = module.getPropertyLevel(0);  //攻击等级
        int defenseLevel = module.getPropertyLevel(1); //防御等级
        int lifeLevel = module.getPropertyLevel(2); //血量等级

        Map<String, skillpoint> allData = DataCenter.getAllData(skillpoint.class);

        HashMap<Integer, skillpoint> map = new HashMap<>();
        for (skillpoint skillpoint : allData.values()) {
            if (skillpoint.getMinlevel() <= attackLevel && attackLevel < skillpoint.getMaxlevel()) {
                map.put(skillpoint.getField(), skillpoint);
            }
        }

        //重置后的增幅为0
        if (map.size() == 0) {
            propertyData.addValue(PropertyType.P_攻击增幅, 0);
            propertyData.addValue(PropertyType.P_防御增幅, 0);
            propertyData.addValue(PropertyType.P_生命增幅, 0);
            playTypeModule.setPlayerCommand(propertyData);
            playTypeModule.set();
            return;
        }

        propertyData.addValue(PropertyType.P_攻击增幅, (long) map.get(1).getAttackscale() * attackLevel);
        propertyData.addValue(PropertyType.P_防御增幅, (long) map.get(2).getPdefensescale() * defenseLevel);
        propertyData.addValue(PropertyType.P_生命增幅, (long) map.get(3).getLifescale() * lifeLevel);

        playTypeModule.setPlayerCommand(propertyData);
        playTypeModule.set();
    }

    //todo ------------------------------------------主公统御加点------------------------------------------


    //todo ------------------------------------------阵营塔-四象归一---------------------------------------
    public static void saveFourSeal(String playerCode, PlayTypeModule playTypeModule) {
        FourSealModule module = FourSealModule.get(playerCode);
        PropertyData propertyData = new PropertyData();
        if (module == null) {
            return;
        }
        //计算四象最低等级
        Set<Map.Entry<Integer, Integer>> entries = module.getFourSeal().entrySet();
        ArrayList<Integer> list = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : entries) {
            list.add(entry.getValue());
        }

        Optional<Integer> min = list.stream().min(Comparator.comparing(Integer::intValue));
        int minlv = min.get();
        //查表racenexusdata最低等级
        racenexusdata rsdata = DataCenter.getData(minlv + 1, racenexusdata.class);
        if (rsdata != null) {
            int attack = rsdata.getAttackscale();//攻击
            int pdefen = rsdata.getPdefensescale();//防御
            int life = rsdata.getLife();//生命
            propertyData.addValue(PropertyType.P_攻击增幅, attack);
            propertyData.addValue(PropertyType.P_防御增幅, pdefen);
            propertyData.addValue(PropertyType.P_生命增幅, life);
        }

        //查表racenexus
        //获取魏国塔等级
        int weilv = module.getFourSeal().get(Constant.WEI);
        //计算魏国塔战力
        racenexus weirsdata = DataCenter.getData(weilv + 1, racenexus.class);
        if (weirsdata != null) {
            propertyData.addValue(PropertyType.P_攻击, weirsdata.getAttack());
            propertyData.addValue(PropertyType.P_防御, weirsdata.getPdefense());
            propertyData.addValue(PropertyType.P_生命, weirsdata.getLifescale());
        }

        //获取蜀国塔等级
        int shulv = module.getFourSeal().get(Constant.SHU) + (Constant.MAX_LV);
        //计算蜀国塔战力
        racenexus shursdata = DataCenter.getData(shulv + 1, racenexus.class);
        if (shursdata != null) {
            propertyData.addValue(PropertyType.P_攻击, shursdata.getAttack());
            propertyData.addValue(PropertyType.P_防御, shursdata.getPdefense());
            propertyData.addValue(PropertyType.P_生命, shursdata.getLifescale());
        }

        //获取吴国塔等级
        int wulv = module.getFourSeal().get(Constant.WU) + (Constant.MAX_LV * 2);
        //计算蜀国塔战力
        racenexus wursdata = DataCenter.getData(wulv + 1, racenexus.class);
        if (wursdata != null) {
            propertyData.addValue(PropertyType.P_攻击, wursdata.getAttack());
            propertyData.addValue(PropertyType.P_防御, wursdata.getPdefense());
            propertyData.addValue(PropertyType.P_生命, wursdata.getLifescale());
        }

        //获取群国塔等级
        int qunlv = module.getFourSeal().get(Constant.QUN) + (Constant.MAX_LV * 3);
        //计算蜀国塔战力
        racenexus qunrsdata = DataCenter.getData(qunlv + 1, racenexus.class);
        if (qunrsdata != null) {
            propertyData.addValue(PropertyType.P_攻击, qunrsdata.getAttack());
            propertyData.addValue(PropertyType.P_防御, qunrsdata.getPdefense());
            propertyData.addValue(PropertyType.P_生命, qunrsdata.getLifescale());
        }

        playTypeModule.setFourSeal(propertyData);
        playTypeModule.set();
    }


    //todo ------------------------------------------阵营塔-四象归一---------------------------------------

//    //todo ------------------------------------------斩关夺隘养成------------------------------------------
//
//    /**
//     * 斩关夺隘养成
//     *
//     * @param playerCode
//     * @param playTypeModule
//     */
//    public static void saveCaptureStrategicPoints(String playerCode, PlayTypeModule playTypeModule) {
//        CaptureStrategicPointsModule module = CaptureStrategicPointsModule.get(playerCode);
//
//        if (module == null) {
//            return;
//        }
//
//        int level = module.getStarsLevel();//等级
//        stargazing data = DataCenter.getData(level, stargazing.class);
//
//        if (data == null) {
//            return;
//        }
//        PropertyData propertyData = new PropertyData();
//        int attackScale = data.getAttackscale();    //攻击百分比
//        int defenseScale = data.getPdefensescale(); //防御百分比
//        int lifeScale = data.getLifescale();        //血量百分比
//        propertyData.addValue(PropertyType.P_攻击增幅, attackScale);
//        propertyData.addValue(PropertyType.P_防御增幅, defenseScale);
//        propertyData.addValue(PropertyType.P_生命增幅, lifeScale);
//
//
//        int attack = data.getAttack();              //攻击
//        int defense = data.getWeight();             //防御
//        int life = data.getLife();                  //血量
//        propertyData.addValue(PropertyType.P_攻击, attack);
//        propertyData.addValue(PropertyType.P_防御, defense);
//        propertyData.addValue(PropertyType.P_生命, life);
//
//        playTypeModule.setCaptureStrategic(propertyData);
//        playTypeModule.set();
//    }
//

    public static void savePassBarrierStars(String playerCode, PlayTypeModule playTypeModule) {
        PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);

        if (module == null) {
            return;
        }

        int level = module.getStarLevelId();//等级
        stargazing data = DataCenter.getData(level, stargazing.class);

        if (data == null) {
            return;
        }
        PropertyData propertyData = new PropertyData();
        int attackScale = data.getAttackscale();    //攻击百分比
        int defenseScale = data.getPdefensescale(); //防御百分比
        int lifeScale = data.getLifescale();        //血量百分比
        propertyData.addValue(PropertyType.P_攻击增幅, attackScale);
        propertyData.addValue(PropertyType.P_防御增幅, defenseScale);
        propertyData.addValue(PropertyType.P_生命增幅, lifeScale);


        int attack = data.getAttack();              //攻击
        int defense = data.getWeight();             //防御
        int life = data.getLife();                  //血量
        propertyData.addValue(PropertyType.P_攻击, attack);
        propertyData.addValue(PropertyType.P_防御, defense);
        propertyData.addValue(PropertyType.P_生命, life);

        playTypeModule.setPassBarrierStars(propertyData);
        playTypeModule.set();
    }


//    //todo ------------------------------------------斩关夺隘养成------------------------------------------


}
